package com.cloudlinkscm.base.filecenter.domain;

import com.cloudlinkscm.base.filecenter.constant.StorageTypeEnum;
import com.cloudlinkscm.base.filecenter.domain.filesystem.FileSystemStorage;
import com.cloudlinkscm.loms.framework.util.SpringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件仓库工厂
 *
 * @author : tac
 * @date : 2017/7/24
 */
public abstract class FileStorageFactory {
    private static Map<StorageTypeEnum, Class<? extends AbstractFileStorage>> clsMap = new HashMap<>();
    static {
        clsMap.put(StorageTypeEnum.FILE_SYSTEM, FileSystemStorage.class);
    }

    public static AbstractFileStorage create(StorageTypeEnum type) {
        if (clsMap.containsKey(type)) {
            Class<? extends AbstractFileStorage> cls = clsMap.get(type);
            AbstractFileStorage storage = SpringUtils.getBean(cls);
            return storage;
        } else {
            return getDefaultStore();
        }
    }

    public static AbstractFileStorage create(String type) {
        return create(StorageTypeEnum.valueOf(type));
    }

    private static AbstractFileStorage getDefaultStore() {
        return SpringUtils.getBean(FileSystemStorage.class);
    }
}
